/**
 * Created by CJB on 2017-01-03 10:52:25.
 */

var loadDiv = null;
	$(function () {
		loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    initUpdateWindow();
});

/**
 * 初始化更新窗口
 * @returns {Function}
 */
function initUpdateWindow(){
    var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
    zTree = zTree.setting.callback.onClick = onDialogTreeOnClick("authorityAddListEdit","parentIdEdit");
    //声明消息提示
    var toolTips = new Object();
    //设置表单验证
    var validator = $("#updateForm").validate({
        rules:{
            name:"required",
            permission:"required",
            authorityAddList:"required"
        },
        messages:{
            name:"请输入名称",
            permission:"请输入标识",
            authorityAddList:"请选择父节点"
        },
        errorPlacement: function(eror,element){
            var toolTip = createTooltip(toolTips,element.attr("id"));
            toolTip.setContent(eror.text());
            element.addClass("error-border");

        },
        success:function(label){
            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
            var toolTip = toolTips[elementId];
            $("#" + elementId).removeClass("error-border");
            toolTip.close();
            toolTip.detach();
        },
        submitHandler:function(form){
        	var parentNodes = getNodeById('parentMenuTree', $("#parentIdEdit").val());
        	var pNodes=parentNodes.getParentNode();
            if($("#parentIdEdit").val() == $("#updateId").val()){
                createWindow('父节点不能是当前节点').dialog('open');
                return ;
            }else if(pNodes){
            	if(pNodes.id==$("#updateId").val()){
            			createWindow('父节点不能是子节点').dialog('open');
                        return ;
            		}}
            loadDiv.showLoading();
            post(getPath() + "base/authority/edit",
                $("#updateForm").serialize(),
                function(resultObject){
            		loadDiv.hideLoading();
                    var errors = resultObject.errors;
                    //判断后台是否有返回错误消息
                    if(errors && errors.length > 0){
                        for(var i=0; i < errors.length; i++){
                            var error = errors[i];
                            var toolTip = createTooltip(toolTips,error.field);
                            toolTip.setContent(error.defaultMessage);
                            $("#" + error.field).addClass("error-border");
                        }
                    }else{
                        //直接更改和移动弹出框的树形数据
                        var node1 = getNodeById('parentMenuTree', $("#updateId").val());
                        var parentNode = getNodeById('parentMenuTree', $("#parentIdEdit").val());
                        node1.name = $("#labelEdit").val();
                        var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
                        zTree.updateNode(node1);
                        var mo = zTree.moveNode(parentNode,node1,'inner');
                        //直接更改和移动左边的树形数据
                        var node2 = getNodeById('menuTree', $("#updateId").val());
                        var menuparentNode = getNodeById('menuTree', $("#parentIdEdit").val());
                        node2.name = $("#labelEdit").val();
                        var zTree = $.fn.zTree.getZTreeObj('menuTree');
                        zTree.updateNode(node2);
                        mo = zTree.moveNode(menuparentNode,node2,'inner');
                        createWindow("更新成功").dialog("open");
                    }
                },function(resultObject){
                	loadDiv.hideLoading();
	        		createWindow(resultObject).dialog('open');
	        	}
            )
        }
    })


    $("#authorityAddListEdit").click({menuContentId:"parentMenu"}, showMenu);
}